﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MySqlQueryToolkit;
using Rhino.Mocks;

namespace MySqlQueryToolkitTests
{
    [TestClass]
    public class ExplainQueryReporterTests
    {
        [TestMethod]
        public void CreateInstanceExpectNoException()
        {
            Assert.IsNotNull(new ExplainQueryReporter(null));
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void ExplainWithNoQueryExpectException()
        {
            var target = new ExplainQueryReporter(null);
            target.Report(string.Empty);
        }

        [TestMethod]
        public void ExplainWithQueryExpectRepositoryCallWithExplainQuery()
        {
            const string query = "SELECT * FROM tblTable";
            const string expected = "EXPLAIN SELECT * FROM tblTable";
            var repositoryMock = MockRepository.GenerateMock<IExplainQueryRepository>();
            var target = new ExplainQueryReporter(repositoryMock);

            target.Report(query);

            repositoryMock.AssertWasCalled(dam => dam.GetEntitiesFromDatabase(expected));
        }

        [TestMethod]
        public void ExplainWithDifferentQueryExpectRepositoryCallWithExplainQuery()
        {
            const string query = "SELECT * FROM tblDifferent;";
            const string expected = "EXPLAIN SELECT * FROM tblDifferent;";
            var repositoryMock = MockRepository.GenerateMock<IExplainQueryRepository>();
            var target = new ExplainQueryReporter(repositoryMock);

            target.Report(query);

            repositoryMock.AssertWasCalled(dam => dam.GetEntitiesFromDatabase(expected));
        }
    }
}
